<?php
/**
 * 《每天生产》实现类
 *
 * @author 孙晓晔
 * @version $Id: Outer.class.php 1053 2010-04-07 11:15:15Z sunxy $
 */
require_once(dirname(__FILE__) . '/MoldUtil.class.php');

class Work extends MoldUtil {
	public $tbl = Constant::tbl_mold_work;

	public $type = array(
		'-' => '请选择',
		'1' => '正班',
		'2' => '加班',
	);

	public $time = array(
		'-' => '请选择',
		'1' => '上午1 07:45',
		'2' => '上午2 08:00',
		'3' => '下午1 13:30',
		'4' => '下午2 14:00',
		'5' => '晚上1 18:00',
		'6' => '晚上2 18:30',
		'7' => '晚上3 19:00',
		'8' => '晚上4 20:00',
	
		'9' => '12:30-13:30',
		'10' => '04:45-07:45',
		'11' => '17:45-19:45',
		'12' => '18:30-20:30',
	);

	public $status = array(
		'-' => '请选择',
		'4' => '等待生产',
		'5' => '正在生产',
		'9' => '完成生产',
	);

	public $orderby = array(
		'-' => '请选择',
		'mold' => '产品',
		'plan' => '计划',
		'emp' => '人员',
		'machine' => '机台',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'mold' => '产品',
		'plan' => '计划',
		'type' => '类型',
		'worker' => '人员',
		'machine' => '机台',
		'emp' => '录入',

		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('time_option', $this->time);
	}
	
	function tab($controller, $request) {		
		$data = array(
			// '生产计划' => array('cc'=>'mold.php?do=Plan.find', 'js'=>''),	
			'每日生产' => array('cc'=>'mold.php?do=Work.find', 'js'=>''),
			// '产品生产' => array('cc'=>'mold.php?do=Produce.find', 'js'=>''),
					
			'产品信息' => array('cc'=>'mold.php?do=Tec.find', 'js'=>''),
		
			'注塑需求' => array('cc'=>'zs.php?do=RequirementMy.find', 'js'=>''),
			'注塑安排' => array('cc'=>'zs.php?do=PlanMy.find', 'js'=>''),
		
			'机台信息' => array('cc'=>'car.php?do=Cnc.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'mold.php?do=Work.tj_tab', 'js'=>''),	
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'产品信息' => array('cc'=>'mold.php?do=Mold.group', 'js'=>''),
			// '生产计划' => array('cc'=>'mold.php?do=Plan.group', 'js'=>''),
				
			'产品生产' => array('cc'=>'mold.php?do=Produce.group', 'js'=>''),
			'每日生产' => array('cc'=>'mold.php?do=Work.group', 'js'=>''),
			'产品质检' => array('cc'=>'mold.php?do=Quality.group', 'js'=>''),			
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'mold/work/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$type_s = $request['type_s'];

		$worker_s = $this->_input($request['worker_s']);

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($date1_s)) {
			$where .= " AND DATE_FORMAT(t1, '%Y-%m-%d') >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND DATE_FORMAT(t2, '%Y-%m-%d') <= '" . $date2_s . "'";
		}
		
		/**
		if (!empty($date1_s)) {
			$where .= " AND DATE_FORMAT(t1, '%Y-%m-%d') <= '" . $date1_s . "' AND DATE_FORMAT(t2, '%Y-%m-%d') >= '" . $date1_s . "'";
		}*/

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		if (!empty($worker_s)) {
			$where .= " AND worker = '" . $worker_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$t1_s = $request['t1_s'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->_tbl($t1_s), $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);				

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'mold/work/list');
		return true;
	}
	
	function _tbl($t1) {
		if (empty($t1)) {
			return $this->tbl;
		}
	
		$dd = date("Y");
		$aa = date("Y", strtotime($t1));
	
		if ($aa == $dd) {
			return $this->tbl;
		}
	
		$dd = date("Y", strtotime($t1));
		return sprintf("%s_%s", $this->tbl, $dd);
	}
	
	function xls($controller, $request) {
		$this->pretreat($controller);
	
		$this->findPara($controller, $request);
	
		$request['tpl'] = "xls";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
	
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$row = $this->_orderby_x($controller, $row, $this->orderby_s);
				
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);
	
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
	
		$controller->display($request, 'mold/work/list');
		return true;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('23')) {
			return '';
		}

		$emp = $_SESSION['id'];

		return " emp = '" . $emp . "'";
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _plan_x($id) {
		if (empty($id)) {
			return '';
		}

		$this->piece = $this->code_option("2");
		$this->technics = $this->code_option("3");

		$row = Common::id(Constant::tbl_mold_plan, $id);

		$row['piece_t'] = $this->piece[$row['piece']];
		$row['technics_t'] = $this->technics[$row['technics']];

		return "[" . $row['piece_t'] . "] " . $row['technics_t'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('3 day'));
		$controller->assign('t2', strtotime('7 day'));

		$controller->display($request, 'mold/work/add');
		return true;
	}

	function exist($tbl, $id) {

	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$mold = $this->_input($request['mold']);
		$plan = $this->_input($request['plan']);

		$type = $request['type'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$machine = $this->_input($request['machine']);
		// $content = sprintf("%s\n%s 添加", addslashes($request['content']), Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);

		$worker = $this->_input($request['worker']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(mold, plan, type, t1, t2, tt, worker, machine, content, emp, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $mold, $plan, $type, $t1, $t2, $tt, $worker, $machine, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		if ($db->affected_rows() > 0) {
			$this->_ret_json(1);
		}
		
		return true;
	}

	function _date($t1, $t2) {
		$t1 = strtotime($t1);
		$t2 = strtotime($t2);

		$date1 = date("Y-m-d", $t1);
		$date2 = date("Y-m-d", $t2);
		if ($date1 == $date2) {
			return sprintf("%s(%s至%s)", $date1, date("H:i", $t1), date("H:i", $t2));
		}

		return sprintf("%s至%s", date("Y-m-d H:i", $t1), date("Y-m-d H:i", $t2));
	}

	function _tt($t1, $t2) {

		return strtotime($t2) - strtotime($t1);
	}
	
	function batch($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('3 day'));
		$controller->assign('t2', strtotime('7 day'));

		$controller->display($request, 'mold/work/batch');
		return true;
	}
	
	function batchPost($controller, $request) {
		$this->pretreat($controller);

		$type = $request['type'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);
		
		$content2 = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);		
		$fmt = "INSERT INTO %s(mold, type, t1, t2, tt, worker, machine, content, emp, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		
		$count = 0;
		$data = json_decode($request['item'], true);
		while (list($k, $v) = each($data)) {
			if (!$this->_ok($v['ok'])) {
				continue;
			}
			
			$mold = $this->_input($v['mold_x']);
			$machine = $this->_input($v['machine']);			
			$worker = $this->_input($v['worker']);
			// $office = $this->_office($worker);
			// $dept = $this->_emp_dept($worker);			
		
			$content = addslashes($v['content']) . "\n" . $content2;
			
			$sql = sprintf($fmt, $this->tbl, $mold, $type, $t1, $t2, $tt, $worker, $machine, $content, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
			if ($db->affected_rows() > 0) {
				$count++;
			}
		}

		$this->_ret_json($count);		
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		$controller->display($request, 'mold/work/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$mold = $this->_input($request['mold']);
		$plan = $this->_input($request['plan']);

		$type = $request['type'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$machine = $this->_input($request['machine']);
		// $content = sprintf("%s\n%s 修改", addslashes($request['content']), Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);

		$worker = $this->_input($request['worker']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET mold = '%s', plan = '%s', type = '%s', t1 = '%s', t2 = '%s', tt = '%s', machine = '%s', content = '%s', ";
		$fmt .= "worker = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mold, $plan, $type, $t1, $t2, $tt, $machine, $content, $worker, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['mold_t'] = Name::mold($row['mold']);
		$row['plan_t'] = $this->_plan_x($row['plan']);

		$row['worker_t'] = Name::emp($row['worker']);
		$row['machine_t'] = Name::machine($row['machine']);

		$row['emp_t'] = Name::emp($row['emp']);

		$controller->assign_by_ref('result', $row);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		$controller->display($request, 'mold/work/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);		

		$controller->assign_by_ref('result', $row);

		require_once 'Mold.class.php';
		$mold = new Mold();
		$controller->assign_by_ref('mold', $mold->_view($row['mold']));

		$controller->display($request, 'mold/work/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['mold_t'] = Name::mold($row['mold']);
		$row['plan_t'] = $this->_plan_x($row['plan']);

		$row['type_t'] = $this->type[$row['type']];
		$row['machine_t'] = Name::machine($row['machine']);
		
		$row['date'] = $this->_date($row['t1'], $row['t2']);		
		$row['tt_t'] = $this->_time_t($row['tt']);
		
		$row['worker_t'] = Name::emp($row['worker']);
		
		$row = $this->_view_t($row);
		
		return $row;
	}

	function finish($controller, $request) {
		$request['status'] = 9;

		parent::status($controller, $request);

		$id = $request['id'];
		$row = $this->id($this->tbl, $id);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mold, '9', $row['mold']);
		// debug($sql);
		$db->query($sql);

		clean(Constant::tbl_mold . "_id_" . $row['mold']);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$num = array(
			'1' => '按次数',
			'2' => '按工时',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'mold/work/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'mold');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
		
		$t1_s = $request['t1_s'];

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(tt) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(tt) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->_tbl($t1_s));
		
		$where = $this->_where($request);
		
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
		
		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'mold') {
			$row['a'] = sprintf("%s [%s]", Name::mold($row['a']), $row['a']);
		} elseif ($px == 'type') {
			$row['a'] = $this->type[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function _num_t($t) {
		return $this->_time_t($t);
	}

	function _time_t($t) {
		$minute = 60;
		$hour = 60 * $minute;
		$day = 24 * $hour;

		return number_format($t / $hour, 2, '.', '');
	}

	function _time_t_bak($t) {
		$minute = 60;
		$hour = 60 * $minute;
		$day = 24 * $hour;

		$ret = '';
		if ($t > $hour) {
			$ret .= floor($t / $hour) . "小时";

			$t = $t % $hour;
		}

		if ($t > $minute) {
			$ret .= floor($t / $minute) . "分钟";

			$t = $t % $minute;
		}

		if ($t > 0) {
			$ret .= $t . "秒";
		}

		return $ret;
	}

	function _time_t2($t) {
		$minute = 60;
		$hour = 60 * $minute;
		$day = 24 * $hour;

		$ret = array();

		if ($t > $day) {
			$ret['d'] = floor($t / $day);

			$t = $t % $day;
		}

		if ($t > $hour) {
			$ret['m'] = floor($t / $hour);

			$t = $t % $hour;
		}

		if ($t > $minute) {
			$ret['i'] = floor($t / $minute);

			$t = $t % $minute;
		}

		if ($t > 0) {
			$ret['s'] = $t;
		}

		return $ret;
	}

	function z($controller, $request) {
		$db = newdb($this->dsn);

		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$emp = $this->_emp($row['content']);

			$fmt = "update %s set emp = '%s' where id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $emp, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function _emp($content) {
		if (preg_match("/李丹/", $content)) {
			return 1198;
		}

		if (preg_match("/胡秋月/", $content)) {
			return 1205;
		}

		if (preg_match("/陈翠红/", $content)) {
			return 1275;
		}

		return 101;
	}
	
	function backup($controller, $request) {
		
		$dd = date("md");
		if ($dd <> '0101') {
			return false;
		}
	
		$dd = date("Y", strtotime("-1 year"));
		$tbl = sprintf("%s_%s", $this->tbl, $dd);
	
		$db = newdb($this->dsn_w);
		$fmt = "ALTER TABLE %s RENAME TO %s";
		$sql = sprintf($fmt, $this->tbl, $tbl);
		$db->query($sql);
	
		$fmt = "CREATE TABLE %s LIKE %s";
		$sql = sprintf($fmt, $this->tbl, $tbl);
		$db->query($sql);
	}
	
	function _test($db, $dd) {
		
		$tbl = sprintf("%s_%s", $this->tbl, $dd);
		
		$fmt = "CREATE TABLE %s LIKE %s";
		$sql = sprintf($fmt, $tbl, $this->tbl);
		echo $sql . "\n";
		$db->query($sql);
		
		$ff = "date_format(createtime, '%Y')";
		$fmt = "INSERT INTO %s SELECT * FROM %s WHERE %s = '%s'";
		$sql = sprintf($fmt, $tbl, $this->tbl, $ff, $dd);
		echo $sql . "\n";
		$db->query($sql);
	}

	function t($controller, $request) {
		
		echo date("md", strtotime("2016-01-01"));

		logdate($controller, $request);
		return true;
	}
	
	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
		
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'mold/work/_list_c');
		return true;
	}
}
?>